﻿Imports System.Runtime.InteropServices.Automation
Imports Microsoft.LightSwitch.Threading
Imports System.Windows

Namespace LightSwitchApplication

    Public Class SubsidiaryLedgerByCategory
        'Sub test()
        '    If AutomationFactory.IsAvailable Then
        '        Dim shell As Dynamic = AutomationFactory.CreateObject("Shell.Application")
        '        shell.execute()
        '    Else

        '    End If
        'End Sub

        Private Sub opendialog_Execute()
            ' Write your code here.
            'Dim file As IO.FileInfo = Nothing

            '' OpenFileDialog() must be opened on the UI thread
            'Dispatchers.Main.Invoke(Sub()
            '                            Dim dlg As New Controls.OpenFileDialog
            '                            dlg.Filter = "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt"

            '                            If dlg.ShowDialog = True Then
            '                                file = dlg.File
            '                            End If
            '                        End Sub)

            ''If (Not file Is Nothing) Then
            ''    PerformImport(file, entitySetToCreateIn)
            ''End If    

            'Property1 = "test"
            'If AutomationFactory.IsAvailable Then
            '    Try
            '        'Dim ps As PacientesSetup = Me.DataWorkspace.ApplicationData.PacientesSetups.[Single](Of PacientesSetup)()

            '        Dim shell = AutomationFactory.CreateObject("Shell.Application")
            '        Dim ObjectFolder = shell.BrowseForFolder(0, "Seleccione la carpeta de usuario", 0, "c:\")
            '        If ObjectFolder IsNot Nothing Then
            '            'Dim objFolderItem = shell.FolderItem = objFolder.Self
            '            Property1 = ObjectFolder.self.path ' ObjectFolder.Title
            '        End If
            '    Catch generatedExceptionName As Exception
            '        Throw New Exception()

            '    End Try
            'Else
            '    Me.ShowMessageBox("Automation not available")
            'End If
        End Sub

        Private Sub AllCategories_Execute()
            ' Write your code here.
            MainAssetsTablepCategoryName = Nothing
        End Sub

        Private Sub SubsidiaryLedgerByCategory_InitializeDataWorkspace(saveChangesTo As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService))
            ' Write your code here.
            MainAssetsTablepCategoryName = Nothing
        End Sub

        Private Sub SubsidiaryLedgerByCategory_Created()
            ' Write your code here.
            Dim comboControl As IContentItemProxy = Me.FindControl("MainAssetsTablepCategoryName")
            comboControl.SetBinding(System.Windows.Controls.ComboBox.ItemsSourceProperty, "Screen.CategoriesAll", System.Windows.Data.BindingMode.OneWay)
            comboControl.SetBinding(System.Windows.Controls.ComboBox.SelectedItemProperty, "Screen.MainAssetsTablepCategoryName", System.Windows.Data.BindingMode.TwoWay)
        End Sub

        Private Sub AssetsByCategory_Loaded(succeeded As Boolean)
            Me.TotalAccumulatedDepreciation = Me.AssetsByCategory.Sum(Function(x) x.AccumulatedDepreciaton)
            Me.SumTotalCost = Me.AssetsByCategory.Sum(Function(x) If(x.AssetAmount > 0, x.AssetAmount, x.BaseCost + x.VAT))
            Me.NetBookValue = Me.SumTotalCost - Me.TotalAccumulatedDepreciation
            Me.TestSum = 0
            For Each asset In AssetsByCategory
                Me.TestSum += If(asset.AssetAmount > 0 _
                                             , asset.AssetAmount _
                                             , If(asset.BaseCost Is Nothing _
                                                   , 0 _
                                                   , asset.BaseCost) + If(asset.VAT Is Nothing _
                                                                          , 0 _
                                                                          , asset.VAT))
            Next
            
        End Sub
    End Class

End Namespace
